package me.zhengjie.business.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.domain.vo.Kb04Vo;
import me.zhengjie.business.domain.vo.ZhgkZpbhVo;
import me.zhengjie.business.repository.Kb04Repository;
import me.zhengjie.business.service.Kb04Service;
import me.zhengjie.business.service.dto.Kb04Dto;
import me.zhengjie.business.service.dto.Kb04QueryCriteria;
import me.zhengjie.utils.DateTimeUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 服务实现
 * @author xiahongbo
 * @date 2023-04-24
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class Kb04ServiceImpl implements Kb04Service {

    private final Kb04Repository kb04Repository;

    @Override
    public Map<String, Object> queryKB04PageList(Kb04QueryCriteria criteria, Pageable pageable) {
        Page<Kb04Vo> page =  kb04Repository.queryKB04PageList(criteria.getAkb020(), criteria.getAae140(), criteria.getCjt100(), criteria.getInsuNo(), criteria.getAae001(), pageable);
        return PageUtil.toPage(page);
    }

    @Override
    public Map<String, Object> queryKB04NewPageList(Kb04QueryCriteria criteria, Pageable pageable) {
        Page<Kb04Vo> page =  kb04Repository.queryKB04NewPageList(criteria.getAkb020(), criteria.getAae140(), criteria.getCjt100(), criteria.getInsuNo(), criteria.getAae001(), pageable);
        return PageUtil.toPage(page);
    }

    @Override
    public void downloadKB04NewList(Kb04QueryCriteria criteria, HttpServletResponse response) throws IOException {
        List<Kb04Vo> Kb04VoList = kb04Repository.queryKB04NewPageList(criteria.getAkb020(), criteria.getAae140(), criteria.getCjt100(), criteria.getInsuNo(), criteria.getAae001());
        List<Map<String, Object>> list = new ArrayList<>();
        int cnt = 0;
        for (Kb04Vo project : Kb04VoList) {
            cnt++;
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("序号", cnt);
            map.put("统筹区", project.getAae002Ks());
            map.put("服务机构编号", project.getAkb020());
            map.put("服务机构名称", project.getAkb021());
            map.put("年度", project.getAae001());
            map.put("参保类型", "0".equals(project.getCjt100()) ? "职工" : "居民");
            map.put("照护类型", "9".equals(project.getAae140()) ? "机构" : "居家");
            map.put("医疗费总额", project.getAkb065());
            map.put("个人帐户金额", project.getAkb066());
            map.put("个人现金金额", project.getAkb067());
            map.put("统筹支付金额", project.getAkb068());
            map.put("预留总金额", project.getAkb090());
            map.put("年底考核扣款金额", project.getAkb071());
            map.put("应支付金额总计", project.getAkb094());
            map.put("拨付机构代码", project.getInsuNo());
            map.put("拨付机构名称", project.getInusName());
            map.put("年度评分", project.getAkb083());
            map.put("总计", project.getId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }


    @Override
    public void download(List<Kb04Dto> all, HttpServletResponse response) throws IOException {

    }
}
